package org.soarsky.wealthchengdu.view;

import java.util.ArrayList;





import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

/**
 * 
 * @author leiyong
 * 
 */
public class PublicDialog extends Dialog {
	public interface IDialogCallback {
		int transmitData(Object data, Object addition);
	}

	protected IDialogCallback mCallback = null;
	protected Object mAdditionData = null;
	private int mLayout;
	private static ArrayList<String> mDialogID = null;
	private String mID = null;

	private void addID() {
		if (mDialogID == null)
			mDialogID = new ArrayList<String>();
		mDialogID.add(mID);
	}

	public PublicDialog(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
	}

	public PublicDialog(String id, Context context, int theme,
			IDialogCallback callback) {
		super(context, theme);
		// TODO Auto-generated constructor stub
		mID = id;
		mCallback = callback;
	}

	public PublicDialog(String id, Context context, int theme,
			Object additionData, IDialogCallback callback) {
		super(context, theme);
		// TODO Auto-generated constructor stub
		mID = id;
		mCallback = callback;
		mAdditionData = additionData;
	}

	public PublicDialog(String id, Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
		mID = id;
	}

	public PublicDialog(String id, Context context, IDialogCallback callback) {
		super(context);
		// TODO Auto-generated constructor stub
		mID = id;
		mCallback = callback;
	}

	public PublicDialog(String id, Context context, Object additionData,
			IDialogCallback callback) {
		super(context);
		// TODO Auto-generated constructor stub
		mID = id;
		mCallback = callback;
		mAdditionData = additionData;
	}

	public PublicDialog(String id, Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mID = id;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if (mID != null && !isOpen(mID)) {
			addID();
		}
		super.onCreate(savedInstanceState);
	}

	public void SetContentView(int layout) {
		mLayout = layout;
		setContentView(mLayout);
	}

	public boolean setOnClickListener(int id, View.OnClickListener listener) {
		View v = this.findViewById(id);
		if (v == null)
			return false;
		v.setOnClickListener(listener);
		return true;
	}

	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		super.dismiss();
		if (mID != null && mDialogID != null) {
			for (int i = 0; i < mDialogID.size(); i++) {
				if (mID.equals(mDialogID.get(i))) {
					mDialogID.remove(i);
					break;
				}
			}
		}
	}

	public static boolean isOpen(String id) {
		if (id != null && mDialogID != null) {
			for (int i = 0; i < mDialogID.size(); i++) {
				if (id.equals(mDialogID.get(i))) {
					//CarApplication.log("newDialog->id is exist. " + id);
					return true;
				}
			}
		}
		return false;
	}

	public static void free() {
		if (mDialogID != null) {
			mDialogID.clear();
			mDialogID = null;
		}
	}

}
